
package ga;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;

public class Graph {

    public Graph() {
    }

    public void fillMatrix(String path) throws FileNotFoundException, IOException {
        BufferedReader br = new BufferedReader(new FileReader(path));
        String line = br.readLine();
        n = Integer.parseInt(line);
        adjacencyMatrix = new int[n][n];
        int j = 0;
        while ((line = br.readLine()) != null) {
            String[] tok = line.split(" ");
            for (int i = 0; i < getN(); i++) {
                if (!tok[i].isEmpty() && Integer.parseInt(tok[i]) != 0) {
                    adjacencyMatrix[j][i] = Integer.parseInt(tok[i]);
                    // чтобы было по строкам ?
                    streets.add(new Pair(j, i));
                }
            }
            j++;
        }
    }

    @Override
    public String toString() {
        String res = "";
        for (int i = 0; i < getN(); i++) {
            for (int j = 0; j < getN(); j++) {
                res = res + adjacencyMatrix[i][j] + " ";
            }
            res = res + "\n";
        }
        return res;
    }

    public int getN() {
        return n;
    }

    public int getEdgeWeight(int i, int j) {
        if (i > -1 && j > -1 && i < n && j < n) {
            return adjacencyMatrix[i][j];
        }
        return -1;
    }

    public ArrayList<Integer> getAdjacencyVertices(int v) {
        ArrayList<Integer> adjacencyVertices = new ArrayList<Integer>();
        for (int j = 0; j < n; j++) {
            if (adjacencyMatrix[v][j] != 0) {
                adjacencyVertices.add(j);
            }
        }
        return adjacencyVertices;
    }

    public int getEdgesCount() {
        return streets.size();
    }

    public ArrayList<Pair> getStreetsList() {
        return streets;
    }

    public void setStreetsList(ArrayList<Pair> streets) {
        this.streets = streets;
    }

    public Path findRandomPath() {
        Path p = new Path();
        int n = getN();
        int edges = getEdgesCount();
        Random rand = new Random();
        int v = rand.nextInt(n); 
        p.addVertex(v);
        int sum = 0;
        int k = 20;
        int maxEdgesCount = rand.nextInt(edges) + 1; 
        while (maxEdgesCount > 0 && p.getPath().size() <= maxEdgesCount && sum <= k) {
            ArrayList<Integer> adjacencyVertices = getAdjacencyVertices(v);
            if (adjacencyVertices.size() > 0) {
                int index = rand.nextInt(adjacencyVertices.size());
                p.addVertex(adjacencyVertices.get(index));
                sum = sum + getEdgeWeight(v, adjacencyVertices.get(index));
                v = adjacencyVertices.get(index);
            } else {
                maxEdgesCount = 0;
            }
        }
        return p;
    }
    
    private int n;
    private int[][] adjacencyMatrix;
    private ArrayList<Pair> streets = new ArrayList<Pair>();
}
